let currentPlayerId = localStorage.getItem('playerId');

function loadScoreRanking() {
    $.get('/api/stats/rankings/score')
        .done(function(rankings) {
            const rankingList = $('#scoreRankingList');
            rankingList.empty();
            
            rankings.forEach((rank, index) => {
                rankingList.append(`
                    <div class="ranking-item d-flex justify-content-between align-items-center p-2 ${rank.playerId === currentPlayerId ? 'bg-light' : ''}">
                        <div class="rank-info">
                            <span class="rank-number ${getRankClass(index + 1)}">${index + 1}</span>
                            <span class="player-name ml-3">${rank.username}</span>
                        </div>
                        <div class="score">${rank.score}分</div>
                    </div>
                `);
            });
        });
}

function loadWinRateRanking() {
    $.get('/api/stats/rankings/winrate')
        .done(function(rankings) {
            const rankingList = $('#winRateRankingList');
            rankingList.empty();
            
            rankings.forEach((rank, index) => {
                rankingList.append(`
                    <div class="ranking-item d-flex justify-content-between align-items-center p-2 ${rank.playerId === currentPlayerId ? 'bg-light' : ''}">
                        <div class="rank-info">
                            <span class="rank-number ${getRankClass(index + 1)}">${index + 1}</span>
                            <span class="player-name ml-3">${rank.username}</span>
                        </div>
                        <div class="win-rate">${rank.winRate.toFixed(1)}%</div>
                    </div>
                `);
            });
        });
}

function loadAchievementRanking() {
    $.get('/api/stats/rankings/achievement')
        .done(function(rankings) {
            const rankingList = $('#achievementRankingList');
            rankingList.empty();
            
            rankings.forEach((rank, index) => {
                rankingList.append(`
                    <div class="ranking-item d-flex justify-content-between align-items-center p-2 ${rank.playerId === currentPlayerId ? 'bg-light' : ''}">
                        <div class="rank-info">
                            <span class="rank-number ${getRankClass(index + 1)}">${index + 1}</span>
                            <span class="player-name ml-3">${rank.username}</span>
                        </div>
                        <div class="achievement-count">${rank.achievementCount}个成就</div>
                    </div>
                `);
            });
        });
}

function getRankClass(rank) {
    switch(rank) {
        case 1: return 'rank-gold';
        case 2: return 'rank-silver';
        case 3: return 'rank-bronze';
        default: return '';
    }
}

// 页面加载完成后初始化
$(document).ready(function() {
    loadScoreRanking();
    
    // 切换标签时加载对应排行榜
    $('#rankingTabs a').on('shown.bs.tab', function(e) {
        const target = $(e.target).attr('href');
        switch(target) {
            case '#scoreRanking':
                loadScoreRanking();
                break;
            case '#winRateRanking':
                loadWinRateRanking();
                break;
            case '#achievementRanking':
                loadAchievementRanking();
                break;
        }
    });
}); 